<template>
	<view class="container">
		<!-- 顶部背景图 -->
		<view class="top-image">
			<image class="orange-img" src="../../static/login-img.jpg"></image>
		</view>

		<!-- 注册表单区域 -->
		<view class="login-from">
			<view class="auth-wrapper">
				<!-- 品牌LOGO -->
				<view class="welcome-icon">
					<image class="welcome-icon-img" src="../../static/logo-text1.png"></image>
				</view>

				<!-- 表单内容 -->
				<view class="form">
					<view class="input-group">
						<!-- 用户名输入 -->
						<view class="input-container">
							<i class="iconfont">&#xe70d;</i>
							<input type="text" v-model="username" placeholder="请输入用户名"
								placeholder-class="placeholder-style" class="input-field" @confirm="register" />
						</view>

						<!-- 密码输入 -->
						<view class="input-container">
							<i class="iconfont">&#xe8b2;</i>
							<input :password="!showPassword" v-model="password" placeholder="请输入密码"
								placeholder-class="placeholder-style" class="input-field" @confirm="register" />
							<view class="icon-wrapper" @click="togglePasswordVisibility('password')">
								<i class="iconfont">{{ passwordVisible ? '&#xe653;' : '&#xe654;' }}</i>
							</view>
						</view>

						<!-- 确认密码输入 -->
						<view class="input-container">
							<i class="iconfont">&#xe8b2;</i>
							<input :password="!showConfirmPassword" v-model="confirmPassword" placeholder="请确认密码"
								placeholder-class="placeholder-style" class="input-field" @confirm="register" />
							<view class="icon-wrapper" @click="togglePasswordVisibility('confirmPassword')">
								<i class="iconfont">{{ confirmPasswordVisible ? '&#xe653;' : '&#xe654;' }}</i>
							</view>
						</view>
					</view>

					<!-- 注册操作区域 -->
					<view class="action-group">
						<button class="action-btn primary" @click="register">立即注册</button>
						<view class="bottom-links">
							<text class="link-text" @click="goToLogin">已有账号？立即登录</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				password: '',
				confirmPassword: '',
				showPassword: false,
				showConfirmPassword: false,
				passwordVisible: false,
				confirmPasswordVisible: false
			};
		},
		methods: {
			register() {
				// 校验用户名和密码是否为空
				if (!this.username) {
					uni.showToast({
						title: '用户名不能为空',
						icon: 'none',
						duration: 2000
					});
					return;
				}

				if (!this.password) {
					uni.showToast({
						title: '密码不能为空',
						icon: 'none',
						duration: 2000
					});
					return;
				}

				// 校验两次密码是否一致
				if (this.password !== this.confirmPassword) {
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none',
						duration: 2000
					});
					return;
				}

				// 发起注册请求
				uni.request({
					url: 'http://localhost:8080/register',
					method: 'POST',
					data: {
						username: this.username,
						password: this.password
					},
					success: (res) => {
						if (res.statusCode === 200 && res.data.code === 200) {
							uni.showToast({
								title: '注册成功',
								icon: 'success',
								duration: 2000
							});
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/login/login'
								});
							}, 2000);
						} else {
							uni.showToast({
								title: res.data.msg || '注册失败',
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail: (err) => {
						console.error('注册接口调用失败', err);
						uni.showToast({
							title: '注册失败',
							icon: 'none',
							duration: 2000
						});
					}
				});
			},

			togglePasswordVisibility(field) {
				if (field === 'password') {
					this.showPassword = !this.showPassword;
					this.passwordVisible = this.showPassword;
				} else {
					this.showConfirmPassword = !this.showConfirmPassword;
					this.confirmPasswordVisible = this.showConfirmPassword;
				}
			},

			goToLogin() {
				uni.redirectTo({
					url: '/pages/login/login'
				});
			}
		}
	};
</script>

<style lang="scss">
	@font-face {
		font-family: 'iconfont';
		/* Project id 4826323 */
		src: url('//at.alicdn.com/t/c/font_4826323_h7hpjyyjlul.woff2?t=1739531070569') format('woff2'),
			url('//at.alicdn.com/t/c/font_4826323_h7hpjyyjlul.woff?t=1739531070569') format('woff'),
			url('//at.alicdn.com/t/c/font_4826323_h7hpjyyjlul.ttf?t=1739531070569') format('truetype');
	}

	.iconfont {
		font-family: "iconfont" !important;
		font-size: 20px;
		font-style: normal;
		-webkit-font-smoothing: antialiased;
		-webkit-text-stroke-width: 0.2px;
		-moz-osx-font-smoothing: grayscale;
		color: #999;
		margin-left: 20rpx;
	}

	.container {
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		background-color: #FFD700;
	}

	.top-image {
		height: 20vh;
		width: 100%;
		background: linear-gradient(to bottom, #FFA500, #FFD700);
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.orange-img {
		width: 100%;
		height: 100%;
	}

	.login-from {
		width: 100%;
		background: linear-gradient(to bottom, #FFA500, #FFD700);
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 5% 0;
	}

	.auth-wrapper {
		width: 90%;
		max-width: 600rpx;
		padding: 40rpx;
		background: #fff;
		border-radius: 25rpx;
		box-shadow: 0 12rpx 24rpx rgba(0, 0, 0, 0.1);
		height: 68vh;
	}

	.welcome-icon {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 40rpx;
	}

	.welcome-icon-img {
		width: 70%;
		height: 100px;
	}

	.input-group {
		margin-bottom: 40rpx;
	}

	.input-container {
		position: relative;
		margin-bottom: 40rpx;
		background: #f8f8f8;
		border-radius: 12rpx;
		transition: all 0.3s ease;
		display: flex;
		align-items: center;

		&:focus-within {
			background: #fff;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
		}
	}

	.input-field {
		width: 100%;
		height: 96rpx;
		padding: 0 32rpx;
		font-size: 32rpx;
		color: #333;
	}

	.placeholder-style {
		color: #999;
		font-size: 32rpx;
	}

	.icon-wrapper {
		position: absolute;
		right: 32rpx;
		top: 50%;
		transform: translateY(-50%);
		padding: 16rpx;
		touch-action: manipulation;
		cursor: pointer;
		z-index: 1000;
	}

	.action-group {
		display: flex;
		flex-direction: column;
		gap: 24rpx;
	}

	.action-btn.primary {
		background: linear-gradient(135deg, #FFA500, #FFD700);
		color: #fff;
		border: none;
		border-radius: 50rpx;
		height: 88rpx;
		font-size: 34rpx;
		width: 100%;
		transition: all 0.3s ease;

		&:active {
			transform: scale(0.98);
			opacity: 0.9;
		}
	}

	.bottom-links {
		text-align: center;
		margin-top: 24rpx;
	}

	.link-text {
		font-size: 28rpx;
		color: #FFA500;
		text-decoration: underline;
		cursor: pointer;
		transition: color 0.3s ease;

		&:hover {
			color: #FFD700;
		}
	}
</style>
